package com.ys.http.network;

import okhttp3.FormBody;

public class FormBuilder {

    private FormBody.Builder builder;

    private FormBuilder() {
    }

    public static FormBuilder newFormBody() {
        FormBuilder form = new FormBuilder();
        form.builder = new FormBody.Builder();
        return form;
    }

    public FormBuilder addParam(String name, String value) {
        if (isValidate(name, value)) {
            builder.add(name, value);
        }
        return this;
    }

    public FormBuilder addParam(String name, Number value) {
        if (name != null) {
            builder.add(name, String.valueOf(value));
        }
        return this;
    }

    public FormBuilder addParam(String name, boolean value) {
        if (name != null) {
            builder.add(name, String.valueOf(value));
        }
        return this;
    }

    public FormBuilder addEncoded(String name, String value) {
        if (isValidate(name, value)) {
            builder.addEncoded(name, value);
        }
        return this;
    }

    public Body build() {
        return new Body(builder.build());
    }

    private boolean isValidate(String name, String value) {
        return name != null && value != null;
    }
}
